package svgedit.gui.command;

import java.util.ArrayList;

import svgedit.gui.Frame;

/**
 * Stack for all command objects
 *     All command will be pushed into a stack, 
 *       when undo these commands will be pop up in FIFO sequence
 * */
public class CommandStack {
	private final ArrayList<Command> commands = new ArrayList<Command>();
	private int currentLocation=-1;
	private int saveLocation = currentLocation;
	
	/** Put all commands into a container , using currentLocation to record the step
	 * 
	 * @param command to get the parameter of command */
	public void addCommand(Command command){
		clearInFrontOfCurrent();
		command.execute();
		commands.add(command);
		currentLocation++;
	}
	
	public int getsize(){
		return commands.size();
	}
	/** Remove command from command stack
	 * 
	 * @param command to get the parameter of command
	 * */
	public void removeCommand(Command command){
		currentLocation--;
		clearInFrontOfCurrent();
	}
	
	/** Remove commands which are in front of current command*/
	private void clearInFrontOfCurrent() {
		while (currentLocation < commands.size() - 1) {
			commands.remove(currentLocation + 1);
		}
	}
	
	/** Undo the command and set the current location to the last command*/
	public void undo() {
		commands.get(currentLocation).undo();
		currentLocation--;
	}

	/** To justify whether undo enabled */
	public boolean undoEnabled() {
		return currentLocation >= 0;
	}

	/** Re do the command that just did which follows undo*/
	public void redo() {
		currentLocation++;
		commands.get(currentLocation).execute();
	}

	/** To justify whether redo enabled*/
	public boolean redoEnabled() {
		return currentLocation < commands.size() - 1;
	}

	public boolean dirty() {
		return currentLocation != saveLocation;
	}

}
